package com.example.test.service;

import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.AppUtils;
import com.example.test.tools.MyUtils;
import com.xdandroid.hellodaemon.AbsWorkService;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.disposables.Disposable;

public class TraceServiceImpl extends AbsWorkService {
    private static final String TAG = TraceServiceImpl.class.getSimpleName();
    //是否 任务完成, 不再需要服务运行?
    public static boolean sShouldStopService;
    public static Disposable sDisposable;

    @Override
    public void onCreate() {
        super.onCreate();
        MyUtils.writeLogcat(TAG, "启动保活服务");
    }

    /**
     * 是否 任务完成, 不再需要服务运行?
     *
     * @return 应当停止服务, true; 应当启动服务, false; 无法判断, 什么也不做, null.
     */
    @Override
    public Boolean shouldStopService(Intent intent, int flags, int startId) {
        return sShouldStopService;
    }

    @Override
    public void startWork(Intent intent, int flags, int startId) {
        sDisposable = Observable.interval(5, TimeUnit.SECONDS)
                .doOnDispose(() -> {//取消任务时取消定时唤醒
                    StringBuilder sb = new StringBuilder();
                    sb.append("\n");
                    sb.append("==============================\n");
                    sb.append("==============================\n");
                    sb.append("==========正常退出APP==========\n");
                    sb.append("==========停止保活服务==========\n");
                    sb.append("==============================\n");
                    sb.append("==============================");
                    MyUtils.writeLogcat(TAG, sb.toString());
                    cancelJobAlarmSub();
                })
                .subscribe(count -> {
                    if (!AppUtils.isAppForeground()) {
                        //MyUtils.writeLogcat(TAG, "APP不在前台运行");
                        AppUtils.launchApp(AppUtils.getAppPackageName());
                    } //else MyUtils.writeLogcat(TAG, "APP正在前台运行");
                });
    }

    @Override
    public void stopWork(Intent intent, int flags, int startId) {
        stopService();
    }

    public static void stopService() {
        //我们现在不再需要服务运行了, 将标志位置为 true
        sShouldStopService = true;
        //取消对任务的订阅
        if (sDisposable != null) sDisposable.dispose();
        //取消 Job / Alarm / Subscription
        cancelJobAlarmSub();
    }

    /**
     * 任务是否正在运行?
     *
     * @return 任务正在运行, true; 任务当前不在运行, false; 无法判断, 什么也不做, null.
     */
    @Override
    public Boolean isWorkRunning(Intent intent, int flags, int startId) {
        //若还没有取消订阅, 就说明任务仍在运行.
        return sDisposable != null && !sDisposable.isDisposed();
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent, Void alwaysNull) {
        return null;
    }

    @Override
    public void onServiceKilled(Intent rootIntent) {
        StringBuilder sb = new StringBuilder();
        sb.append("\n");
        sb.append("==============================\n");
        sb.append("==============================\n");
        sb.append("==========APP异常退出==========\n");
        sb.append("==============================\n");
        sb.append("==============================");
        MyUtils.writeLogcat(TAG, sb.toString());
    }
}
